/*
ID:nacci521
PROG:gift1
LANG:C++14
*/
#include <map>
#include <vector>
#include <fstream>
#include <string>
#include <iostream>

using namespace std;



int main(int argc, char *argv[]) {
  ifstream ifs("Gift1.in");
  vector<string> names;
  
  int np = 0;
  ifs >> np;
  
  map<string, int> group;
  string name;
  
  for (int i = 0; i < np; ++i) {
    ifs >> name;
    names.push_back(name);
    group[name] = 0;
  }
  
  while (!ifs.eof()) {
    ifs >> name;
    
    int money = 0, ng = 0;
    ifs >> money >> ng;

    if (ng != 0) {
      int gift = money / ng;
      group[name] -= money;

      // Give gifts
      for (int i = 0; i < ng; ++i) {
        string receiver;
        ifs >> receiver;
        group[receiver] += gift;

        cout<< endl;
        cout << name << " gives " << receiver << " " << gift << endl;
        cout << name << " remains: " << group[name] << endl;
        cout<< endl;
      }

      group[name] += money % ng;
      cout << name << " get extra: " << money % ng << endl;
      cout << name << " has: " << group[name] << endl;
    }
    else {
      group[name] += money;
      cout << name << " get: " << money << " himself." << endl;
    }
  }
  
  ofstream ofs("Gift1.out");
  
  for (auto people : names) {
    ofs << people << " " << group[people] << endl;
  }
  
  return 0;
}